package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.LocalStoreInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 本地商城
 * Created by huyang on 2016/1/11 0011.
 */
public class LocalStoreModularGridAdapter extends BaseAdapter {

    private Context context;
    private List<LocalStoreInfo> data;

    public LocalStoreModularGridAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public LocalStoreModularGridAdapter(Context context, List<LocalStoreInfo> data) {
        this.context = context;
        this.data = data;
    }

    public void setData(List<LocalStoreInfo> data) {
        this.data.clear();
        this.data.addAll(data);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LocalStoreInfo info = data.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.grid_item_localstore_modular, null);
            holder = new ViewHolder();
            holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_modular_icon);
            holder.tv_label = (TextView) convertView.findViewById(R.id.tv_modular_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_label.setText(info.getLabel());
        ImageLoader.getInstance().displayImage(info.getIcon(),holder.iv_icon);
        return convertView;
    }

    class ViewHolder {
        ImageView iv_icon;
        TextView tv_label;
    }
}
